package com.test.com.bluetoothchatandroid.activity;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Process;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.test.com.bluetoothchatandroid.R;
import com.test.com.bluetoothchatandroid.adapter.GroupFriendAdapter;
import com.test.com.bluetoothchatandroid.base.BaseActivity;
import com.test.com.bluetoothchatandroid.entity.FriendInfo;
import com.test.com.bluetoothchatandroid.entity.GroupInfo;
import com.test.com.bluetoothchatandroid.global.Constant;
import com.test.com.bluetoothchatandroid.utils.ActivityUtil;
import com.test.com.bluetoothchatandroid.utils.AppManager;
import com.test.com.bluetoothchatandroid.utils.BluetoothUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author saviorT
 */
public class MainActivity extends BaseActivity {


	@BindView(R.id.center_title)
	TextView centerTitle;
	@BindView(R.id.toolbar)
	Toolbar toolbar;
	@BindView(R.id.expand_listView)
	ExpandableListView expandListView;
	@BindView(R.id.noteTv)
	TextView noteTv;

	private GroupFriendAdapter mAdapter;
	private List<GroupInfo> mGroupFriendListData = new ArrayList<>();


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ButterKnife.bind(this);

		initView();
	}

	private void initView() {
		centerTitle.setText("好友列表");
		initData();
	}

	private void initData() {
		// if this device not support BlueTooth will exit ag
		if (BluetoothUtil.isSupportBle(mContext)) {
			BluetoothUtil.enableBluetooth((Activity) mContext, 1);
		} else {
			showToast("Sorry for that this phone do not support BlueTooth");
			new Handler().postDelayed(() ->
					AppManager.getAppManager().appExit(), 3000);
		}
		mAdapter = new GroupFriendAdapter(mContext, mGroupFriendListData);
		expandListView.setAdapter(mAdapter);
		expandListView.expandGroup(0);
		expandListView.setOnChildClickListener((expandableListView, view, groupPosition, childPosition, l) -> {
			FriendInfo friendInfo = mGroupFriendListData.get(groupPosition).getFriendList().get(childPosition);
			Bundle bundle = new Bundle();
			bundle.putParcelable(Constant.FRIEND_INFO, friendInfo);
			ActivityUtil.startForwardActivity(MainActivity.this, ChatActivity.class, bundle, false);
			return true;
		});
	}


	@OnClick({R.id.expandBtn, R.id.nothingBtn})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.expandBtn:
				startActivity(new Intent(this, ExpandListActivity.class));
				break;
			case R.id.nothingBtn:
				startActivity(new Intent(this, AddFriendActivity.class));
				break;
			default:
				break;
		}
	}


	private void getHasPairedDevice() {
//		Set:注重独一无二的性质,该体系集合可以知道某物是否已近存在于集合中,不会存储重复的元素
		Set<BluetoothDevice> pairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
		if (pairedDevices.size() > 0) {
			mGroupFriendListData.clear();
			GroupInfo groupInfo = new GroupInfo();
			groupInfo.setGroupName(BluetoothAdapter.getDefaultAdapter().getName());
			List<FriendInfo> friendInfoList = new ArrayList<>();
			for (BluetoothDevice device : pairedDevices) {
				FriendInfo friendInfo = new FriendInfo();
				friendInfo.setIdentificationName(device.getName());
				friendInfo.setDeviceAddress(device.getAddress());
				friendInfo.setFriendNickName(device.getName());
				friendInfo.setOnline(false);//need change this states dynamically
//				friendInfo.setJoinTime()
				friendInfo.setBluetoothDevice(device);
				friendInfoList.add(friendInfo);
			}

			groupInfo.setFriendList(friendInfoList);
			groupInfo.setOnlineNumber(friendInfoList.size());
			mGroupFriendListData.add(groupInfo);
			if (friendInfoList.size() == 0) {
				expandListView.setVisibility(View.GONE);
				noteTv.setVisibility(View.VISIBLE);
			}else {
				expandListView.setVisibility(View.VISIBLE);
				noteTv.setVisibility(View.GONE);
			}
			// set adapter
			mAdapter.setGroupInfoList(mGroupFriendListData);

		}
	}

	private long exitTime;

	@Override
	public void onBackPressed() {
		if ((System.currentTimeMillis() - exitTime) > 2000) {
			Toast.makeText(getApplicationContext(), "再按一次退出应用", Toast.LENGTH_SHORT).show();
			exitTime = System.currentTimeMillis();
		} else {
			ExitApp();
		}
	}

	private void ExitApp() {
		this.finish();
		Process.killProcess(Process.myPid());
		System.exit(0);
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1) {
			if (resultCode == RESULT_OK) {
				getHasPairedDevice();
			} else {
				AppManager.getAppManager().appExit();
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onRestart() {
		if (BluetoothUtil.isSupportBle(mContext)) {
			BluetoothUtil.enableBluetooth((Activity) mContext, 1);
		} else {
			showToast("该手机不支持蓝牙,3秒后应用将自动退出");
			new Handler().postDelayed(() -> AppManager.getAppManager().appExit(), 3000);
		}
		super.onRestart();

	}
}
